/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import { EntityRepository, Repository } from "typeorm";
import { v4 } from "uuid";
import { WinnerEntity } from "../entity/WinnerEntity";

@EntityRepository(WinnerEntity)
export class WinnerRepository extends Repository<WinnerEntity> {

    add(winner : WinnerEntity) {
    	winner.id = v4();
        winner.create = new Date();

        this.save(winner);
    }


	queryByBingo(bingoId : string) {
		return this.createQueryBuilder("winner")
				.innerJoinAndSelect("winner.prize", "prize")
				.innerJoinAndSelect("winner.member", "member")
				.where("winner.prize.bid = :_bingoId", {_bingoId: bingoId})
				.addOrderBy("winner.prize.pid", "ASC")
            	.addOrderBy("winner.create", "DESC")
		    .getMany();
	}

    queryByPrize(bingoId : string, prizeId : number) {
        return this.createQueryBuilder("winner")
				.innerJoinAndSelect("winner.prize", "prize")
				.innerJoinAndSelect("winner.member", "member")
				.where("winner.prize.bid = :_bingoId AND winner.prize.pid = :_prizeId",
					{_bingoId: bingoId, _prizeId: prizeId})
				.addOrderBy("winner.create", "ASC")
            .getMany();
    }
	
}